package org.opsli.modulars.WeChatConfig;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
import org.opsli.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Configuration
@EnableConfigurationProperties({WxMiniLoginProperties.class, WxMpPayProperties.class,WxMpProperties.class})
public class WxMpConfig {

    @Autowired

    private final WxMiniLoginProperties loginProperties;
    @Autowired

    private final WxMpPayProperties payProperties;
    @Autowired

    private final WxMpProperties properties;
    public WxMpConfig(WxMiniLoginProperties loginProperties, WxMpPayProperties payProperties ,WxMpProperties properties) {
        this.loginProperties = loginProperties;
        this.payProperties = payProperties;
        this.properties = properties;
    }

    /**
     * 与微信小程序有关的总service
     * @return WxMaService
     */
    @Bean
    public WxMaService wxMaService() {
        List<WxMiniLoginProperties.Config> configs = this.loginProperties.getConfigs();
        if (configs == null) {
            throw new ServiceException(500,"没有微信小程序配置啊");
        }
        WxMaService wxService = new WxMaServiceImpl();
        Map<String, WxMaConfig> multiConfigs = configs.stream()
                .map(cof -> {
                    WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
                    config.setAppid(cof.getAppid());
                    config.setSecret(cof.getSecret());
                    config.setToken(cof.getToken());
                    config.setAesKey(cof.getAesKey());
                    config.setMsgDataFormat(cof.getMsgDataFormat());
                    return config;
                })
                .collect(Collectors.toMap(WxMaDefaultConfigImpl::getAppid, Function.identity(), (o, n) -> o));
        wxService.setMultiConfigs(multiConfigs);
        return wxService;
    }

    @Bean
    public WxMpService wxMpService() {
        // 代码里 getConfigs()处报错的同学，请注意仔细阅读项目说明，你的IDE需要引入lombok插件！！！！
        final List<WxMpProperties.MpConfig> configs = this.properties.getConfigs();
        if (configs == null) {
            throw new RuntimeException("大哥，拜托先看下项目首页的说明（readme文件），添加下相关配置，注意别配错了！");
        }

        WxMpService service = new WxMpServiceImpl();
        service.setMultiConfigStorages(configs
                .stream().map(a -> {
                    WxMpDefaultConfigImpl configStorage;
                    configStorage = new WxMpDefaultConfigImpl();
                    configStorage.setAppId(a.getAppId());
                    configStorage.setSecret(a.getSecret());
                    configStorage.setToken(a.getToken());
                    configStorage.setAesKey(a.getAesKey());
                    return configStorage;
                }).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
        return service;
    }


    @Bean
    public WxPayService wxPayService() {
        List<WxMpPayProperties.Config> configs = this.payProperties.getConfigs();
        if (configs == null) {

            throw new ServiceException(500,"没有微信小程序配置啊");
        }
        WxPayService wxPayService = new WxPayServiceImpl();
        return wxPayService;
    }

}
